(function($){
        
	    $.fn.extend({
	        jvitLoadPage: function(option) {
	           
               
                var self = this;
                var currentPage = null;
                var nextPage = null;
                var currentElement = null;
                var getTimeout = null;
                var maxPage = 0;
                
                var defaults = {
	                slideProperties: {},
                    autoRepeat: true
	            }
	            var options =  $.extend(defaults, option);
                init();
                
                function init()
                {
                    for(var i = 0; i < Object.keys(options.slideProperties).length; i++) {
                        if(currentPage == null && i == 0)
                        {
                            currentPage = options.slideProperties[0].link;
                            timeCurrent = options.slideProperties[0].timeout;
                            currentElement = i;
                            if(options.slideProperties[1] == undefined)
                            {
                                nextPage = options.slideProperties[0].link;
                            }else
                            {
                                nextPage = options.slideProperties[1].link;
                            }
                        }
                        maxPage = maxPage + 1;
                        
                    }
                    var contentHtml = getContent(currentPage);
                    $(self).html(contentHtml).hide().show(1000);           
                    getTimeout = setInterval(autoAdvance, timeCurrent); 
                    
                }
                
                function autoAdvance()
                {
                    
                    if('wrapper' == $(self).attr('id')) {
                        console.log(options);
                    }
                    window.clearInterval(getTimeout);
                    if(options.slideProperties[currentElement + 1] == undefined)
                    {
                        currentElement = 0;
                    }else {
                        currentElement = currentElement + 1;
                    }
                    var contentHtml = getContent(options.slideProperties[currentElement].link);
                    var timeCurrent = options.slideProperties[currentElement].timeout;
                    $(self).html(contentHtml).hide().show(1000);  
                    getTimeout = setInterval(autoAdvance, timeCurrent); 
               
                    if(options.autoRepeat == false && (currentElement + 1 == maxPage))
                    {
                         window.clearInterval(getTimeout);
                    }
                }
                
                
                function getContent(link)
                {
                    bodyContent = $.ajax({
                          url: link,
                          global: false,
                          type: "POST",
                          dataType: "html",
                          async:false
                       } 
                    ).responseText;
                    return bodyContent;
                }
	        }
	    });       
})(jQuery);



